package com.itheima.dao.Repository;

import com.itheima.model.domain.BusGoodsInfo;
import com.itheima.model.domain.BusUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import javax.transaction.Transactional;
import java.util.List;

public interface BusGoodsResponsitory extends  JpaRepository<BusGoodsInfo,Integer> {

    List<BusGoodsInfo> findAll();

    @Query(value = "select  a from BusGoodsInfo a where a.busUser_goods_id.bus_user_id=:bus_user_id")
    List<BusGoodsInfo> AllGoodsById(@Param("bus_user_id")int bus_user_id);
    @Query(value ="select a from BusGoodsInfo a where a.busUser_goods_id.bus_user_name=:bus_user_name")
    List<BusGoodsInfo> findGoodsByName(@Param("bus_user_name") String bus_user_name);

    @Transactional
    @Modifying
    @Query("DELETE FROM BusGoodsInfo a WHERE a.goods_id = :id")
    void deleteBusGoodsInfoById(int id);

    @Query(value="SELECT a FROM BusGoodsInfo a WHERE a.goods_name LIKE %:keyword%")
    List<BusGoodsInfo> findByGoodsnameContaining(String keyword);

    @Query(value = "select a from BusGoodsInfo  a where a.goods_id=:id")
    BusGoodsInfo findBusGoodsById(@Param("id")int id);

    @Transactional
    @Modifying
    @Query("UPDATE BusGoodsInfo a SET a.goods_name = :#{#goods.goods_name}, a.goods_content= :#{#goods.goods_content} , a.goods_priece = :#{#goods.goods_priece} , a.goods_type = :#{#goods.goods_type}  WHERE a.goods_id = :#{#goods.goods_id}")
    void updateBusUserInfo(@Param("goods") BusGoodsInfo goods);
}
